package cz.proks.bank.generator;

import java.io.ByteArrayOutputStream;

import javax.activation.DataHandler;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.sun.istack.ByteArrayDataSource;

import cz.proks.bank.service.domain.ReportRequest;
import cz.proks.bank.service.domain.ReportRequest.AccountRecord;

public class PdfGenerator implements Generator {
	private static Font titleFont = new Font(Font.TIMES_ROMAN, 18, Font.BOLD);
	private static Font defaultFont = new Font(Font.TIMES_ROMAN, 14, Font.BOLD);
	
	
	@Override
	public DataHandler generate(String bankCode, Integer accountNumber, AccountRecord[] recourds) throws Exception {		
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			
			Document document = new Document();
			
			PdfWriter.getInstance(document, bos);
			
			document.open();
			
			addMetaData(document);
			addContent(document, bankCode, accountNumber);
			addLogs(document, recourds);	
			
			document.close();
			
			DataHandler dataHandler = new DataHandler(new ByteArrayDataSource(bos.toByteArray(), "application/pdf"));
			
			return dataHandler;
	} 
	
	private void addMetaData(Document document) {
		document.addTitle("Account report");
		document.addSubject("Generated account report");
		document.addKeywords("proks-bank, account report,iText, pdf");
		document.addAuthor("Martin Proks");
		document.addCreator("Martin Proks");
	}
	
	private void addContent(Document document, String bankCode, Integer accountNumber) throws DocumentException {
		Paragraph preface = new Paragraph();
		
		addEmptyLine(preface, 1);
		
		Paragraph title = new Paragraph("Bank report for account " + accountNumber+ " / " + bankCode, titleFont);
		
		preface.add(title);
		
		addEmptyLine(preface, 4);		
		
		document.add(preface);
	}
	
	private static void addLogs(Document document, AccountRecord[] records) throws DocumentException {
		Paragraph paragraph = new Paragraph();
		PdfPTable table = new PdfPTable(3);
		
		table.getDefaultCell().setBorder(Rectangle.NO_BORDER);		

		PdfPCell dateCell = new PdfPCell(new Phrase("Date"));
		dateCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(dateCell);

		PdfPCell operationCell = new PdfPCell(new Phrase("Operation"));
		operationCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(operationCell);

		PdfPCell infoCell = new PdfPCell(new Phrase("Information"));
		infoCell.setHorizontalAlignment(Element.ALIGN_CENTER);
		table.addCell(infoCell);
		
		table.setHeaderRows(1);

		if (records != null)
			for (AccountRecord accountRecord : records) {
				table.addCell(accountRecord.getDate().toString());
				table.addCell(accountRecord.getOperationType().toString());
				table.addCell(accountRecord.getAdditionalInformation());			
			}

		paragraph.add(table);
		document.add(paragraph);
	}
	
	private void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
}
